
 /**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */
package com.sys.midware.redis.core.operation.impl;

import java.util.Set;

import org.apache.log4j.Logger;

import com.sys.midware.redis.core.RedisFactory;
import com.sys.midware.redis.core.operation.AbstractOperations;
import com.sys.midware.redis.core.operation.ZSetOperations;
import com.sys.midware.redis.exceptions.BRedisException;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

/**
 * <b>Description：</b> ZSet 类型操作类 <br/>
 * <b>ClassName：</b> HashOperationsImpl <br/>
 * <b>@author：</b> jackyshang <br/>
 * <b>@date：</b> 2016年8月22日 下午4:11:23 <br/>
 * <b>@version: </b>  <br/>
 */
public class ZSetOperationsImpl extends AbstractOperations implements ZSetOperations{

    private static Logger logger = Logger.getLogger(StringsOperationsImpl.class);
    private ZSetOperationsImpl() {
    };

    private static ZSetOperationsImpl instance;
    
    /**
     * 获取单例工厂
     * @return
     * @throws Exception
     */
    public static ZSetOperationsImpl getInstance() {
        if (instance != null)
            return instance;

        synchronized (ZSetOperationsImpl.class) {
            if (instance == null) {
                instance = new ZSetOperationsImpl();
            }
            return instance;
        }
    }

    @Override
    public <K, V> Long add(K key, Set<ZSetTuple<V>> tupleSet) {
        Jedis jedis = getClient();
        try {
            Long l=jedis.zadd(rawKey(key), rawTupleValues(tupleSet));
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public <K, V> Long add(K key, V value, double score) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zadd(rawKey(key), score, rawValue(value));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Double incrementScore(K key, V value, double delta) {
        Jedis jedis = getClient();
        try {
       Double l=  jedis.zincrby(rawKey(key), delta, rawValue(value));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long size(K key) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zcard(rawKey(key));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long delete(K key, Object... values) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zrem(rawKey(key), rawValues(values));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long delete(K key, long start, long end) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zremrangeByRank(rawKey(key), start, end);
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long deleteByScoreRange(K key, double min, double max) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zremrangeByScore(rawKey(key), min, max);
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Double getScore(K key, Object value) {
        Jedis jedis = getClient();
        try {
            Double l=  jedis.zscore(rawKey(key), rawValue(value));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Long getRank(K key, Object o) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zrank(rawKey(key), rawValue(o));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public <K, V> Long getReverseRank(K key, Object o) {
        Jedis jedis = getClient();
        try {
        Long l=  jedis.zrevrank(rawKey(key), rawValue(o));
            
            return l;
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    @Override
    public <K, V> Set<V> getAll(K key) {
        Jedis jedis = getClient();
        try {
      Set<byte[]> re=  jedis.zrange(rawKey(key), 0, -1);
      
            return deserializeValues(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }


    @Override
    public <K, V> Set<V> getByScoreRange(K key, double min, double max) {
        Jedis jedis = getClient();
        try {
            Set<byte[]> re=  jedis.zrangeByScore(rawKey(key), min, max);
            
            return deserializeValues(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }
    
    @Override
    public <K, V> Set<ZSetTuple<V>> getAllWithScore(K key) {
        Jedis jedis = getClient();
        try {
            Set<Tuple> re=  jedis.zrangeWithScores(rawKey(key), 0, -1);
            
            return deserializeTupleValues(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

    @Override
    public <K, V> Set<ZSetTuple<V>> getByScoreRangeWithScore(K key, double min, double max) {
        Jedis jedis = getClient();
        try {
            Set<Tuple> re=  jedis.zrangeByScoreWithScores(rawKey(key), min, max);
            
                return deserializeTupleValues(re);
        } catch (Exception e) {
            logger.error(e);
            throw new BRedisException(e);
        } finally {
            RedisFactory.close(jedis);
        }
    }

  
    
}
